#!/usr/bin/python
# -*- coding: utf-8 -*-
#########################################################################
## This scaffolding model makes your app work on Google App Engine too
#########################################################################

try:
    from gluon.contrib.gql import *  # if running on Google App Engine
except:
    db = SQLDB('sqlite://storage.db')  # if not, use SQLite or other DB
else:
    db = GQLDB()  # connect to Google BigTable
    session.connect(request, response, db=db)  # and store sessions there
    # or use the following lines to store sessions in Memcache
    #from gluon.contrib.memdb import MEMDB
    #from google.appengine.api.memcache import Client
    #session.connect(request, response, db=MEMDB(Client()))

#########################################################################
## uncomment the following line if you do not want sessions
#session.forget()
#########################################################################

#########################################################################
## Define your tables below, for example
##
## >>> db.define_table('mytable',db.Field('myfield','string'))
##
## Fields can be 'string','text','password','integer','double','booelan'
##       'date','time','datetime','blob','upload', 'reference TABLENAME'
## There is an implicit 'id integer autoincrement' field
## Consult manual for more options, validators, etc.
##
## More API examples for controllers:
##
## >>> db.mytable.insert(myfield='value')
## >>> rows=db(db.mytbale.myfield=='value).select(db.mytable.ALL)
## >>> for row in rows: print row.id, row.myfield
#########################################################################
db.define_table('company_info',
    db.Field('name',length=512,default='DCRS Solutions'),
    db.Field('address',default='2605 Metro Blvd'),
    db.Field('city',default='Maryland Heights'),
    db.Field('state',length=2,default='MO'),
    db.Field('zip_code',length=10,default='63043'),
    db.Field('phone',default='314-739-6666'),
    db.Field('fax',default='314-739-8619'),
    db.Field('email',requires=IS_EMAIL(),default='proposals@dcrs.com'),
    db.Field('logo','upload',default=''),
    db.Field('color_background',length=10,default='white'),
    db.Field('color_foreground',length=10,default='black'),
    db.Field('color_header',length=10,default='#339900'),
    db.Field('color_link',length=10,default='#ff0033'),
    db.Field('font_family',length=32,default='arial, helvetica'))

db.define_table('chart_of_accounts',
    db.Field('account_number','integer'),
    db.Field('description',length=30))
    
db.define_table('freight_type',
    db.Field('name',length=512))

db.define_table('support_type',
    db.Field('name',length=25),
    db.Field('code',length=2))
    
db.define_table('document_category',
    db.Field('name',length=512))

db.define_table('document',
    db.Field('name',length=512),
    db.Field('document_category',db.document_category),
    db.Field('file','upload',default=''),
    db.Field('description','text'))

db.define_table('included_text',
    db.Field('name',length=512),
    db.Field('comments','text'))

db.define_table('product_category',
    db.Field('number','integer',length=4),
    db.Field('name',length=50),
    db.Field('headline',length=512),
    db.Field('comments','text'))
    
db.define_table('product',
    db.Field('item',length=12),
    db.Field('name',length=30),
    db.Field('product_category',db.product_category),
    db.Field('short_description',length=512,default=''),
    db.Field('long_description','text'),
    db.Field('account',db.chart_of_accounts),
    db.Field('image','upload',default=''),
    db.Field('quantity_in_stock','integer',default=0),
    db.Field('retail','double',default=0.0),
    db.Field('cost','double',default=0.0),
    db.Field('margin','double',default=0.0),
    db.Field('zone1','double',default=0.0),
    db.Field('phone_support','double',default=0.0),
    db.Field('support_amount','double',default=0.0),
    db.Field('train_amount','double',default=0.0),
    db.Field('taxable','boolean',default='T'),
    db.Field('freight_amount','double',default=0.0),
    db.Field('price_code',length=4),
    db.Field('maintenance_discount','boolean'),
    db.Field('wiring_agreement','boolean'),
    db.Field('license_agreement','boolean'),
    db.Field('purchase_agreement','boolean'),
    db.Field('included_texts','text'),
    db.Field('created','datetime',default=request.now))

db.product.product_category.requires=IS_IN_DB(db,'product_category.id','%(name)s',
orderby=db.product_category.number)
#db.product.freight_type.requires=IS_IN_DB(db,'freight_type.id','freight_type.name')
#db.product.support_type.requires=IS_IN_DB(db,'support_type.id','support_type.name')
db.product.name.requires=IS_NOT_EMPTY()
db.product.short_description.requires=IS_NOT_EMPTY()
db.product.quantity_in_stock.requires=IS_INT_IN_RANGE(0,1000)
db.product.cost.requires=IS_FLOAT_IN_RANGE(0,10000)
db.product.margin.requires=IS_FLOAT_IN_RANGE(0,10000)
db.product.retail.requires=IS_FLOAT_IN_RANGE(0,10000)
#db.comment.product.requires=IS_IN_DB(db,'product.id','product.name')
#db.comment.body.requires=IS_NOT_EMPTY()

db.document.document_category.requires=IS_IN_DB(db,'document_category.id',
'%(name)s')
db.document.name.requires=IS_NOT_EMPTY()
db.product.included_texts.requires=IS_IN_DB(db,
'included_text.id','%(name)s',multiple=True)
#########################################################################
## Here is sample code if you need:
## - email capabilities
## - authentication (registration, login, logout, ... )
## - authorization (role based authorization)
## - crud actions
## uncomment as needed
#########################################################################

from gluon.tools import Mail, Auth, Crud            # new in web2py 1.56
mail=Mail()                                         # mailer
mail.settings.server='smtp.gmail.com:587'           # your SMTP server
mail.settings.sender='webdev@dcrs.com'              # your email
mail.settings.login='webdev@dcrs.com:datacash672'   # your credentials
auth=Auth(globals(),db)                             # authentication/authorization
auth.settings.mailer=mail                           # for user email verification
auth.define_tables()                                # creates all needed tables
crud=Crud(globals(),db)                             # for CRUD helpers using auth
#crud.settings.auth=auth                            # (optional) 
                                                    # enforces authorization on crud
#########################################################################
## then, to expose authentication
## http://..../[app]/default/user/login
## http://..../[app]/default/user/logout
## http://..../[app]/default/user/register
## http://..../[app]/default/user/profile
## http://..../[app]/default/user/retrieve_password
## http://..../[app]/default/user/change_password
## use the following action in controller default.py
##
##     def user(): return dict(form=auth())
##
## read docs for howto create roles/groups, assign memberships and permissions
##
## to expose CRUD
## http://..../[app]/default/data/tables
## http://..../[app]/default/data/select/[table]
## http://..../[app]/default/data/create/[table]
## http://..../[app]/default/data/read/[table]/[id]
## http://..../[app]/default/data/update/[table]/[id]
## http://..../[app]/default/data/delete/[table]/[id]
## use the following action in controller default.py
##
##     def data(): return dict(form=crud())
##
## to allow automatic download of all uploaded files and enforce authorization
## use the following action in controller default.py
##
##     def download(): return response.download(request,db)
